package com.kiln.juxingqs.ui

import android.content.Intent
import androidx.lifecycle.Observer
import client.global.commlib.toast.toast
import com.kiln.juxingqs.BaseMiddleActivity
import com.kiln.juxingqs.R
import com.kiln.juxingqs.databinding.ActivityZrwtBinding
import com.kiln.juxingqs.dialog.JyInputNumDialog
import com.kiln.juxingqs.dialog.PagingFixedBathDddDialog
import com.kiln.juxingqs.ui.vm.ZrWtViewModel

class ZrWtActivity : BaseMiddleActivity<ZrWtViewModel, ActivityZrwtBinding>() {

    private var goods_id: String? = null

    override fun getLayout(): Int {
        return R.layout.activity_zrwt
    }

    override fun initView() {
        binding?.apply {
            event = this@ZrWtActivity
            vm = viewModel
            goods_id = intent.getStringExtra("goods_id")
        }
    }

    override fun observerUI() {
        viewModel.call.observe(this, Observer {
            it?.apply {
                if (isSuccess) {
                    toast("操作成功")
                    startActivity(Intent(this@ZrWtActivity, XyzrNewActivity::class.java).apply {
                        putExtra("index", 2)
                        putExtra("goods_id", goods_id)//新goods_id
                    })
                    finish()
                }
            }
        })

        viewModel.otherAccount.observe(this, {
            viewModel.otherName.value = ""
        })
        viewModel.queryRet.observe(this, {
            it?.let {
                if (it.isSuccess) {
                    viewModel.otherName.value = it.data?.real_name
                }
            }
        })
    }

    fun selectNum() {
        PagingFixedBathDddDialog(this@ZrWtActivity, goods_id).apply {
            vm = viewModel
            ok = {
                viewModel.selectDddBath.value = it
                JyInputNumDialog(this@ZrWtActivity).apply {
                    ok = { num ->
                        viewModel.sNum.value = "${num}"
                        dismiss()
                    }
                }.show()
            }
        }.show()
    }

    override fun initViewModel(): ZrWtViewModel = createViewModel()

}